package com.oocl.scan.store;

import com.oocl.scan.model.Component;
import com.oocl.scan.service.SaveService;
import java.util.concurrent.ConcurrentLinkedQueue;

public class ComponentStore implements Store<Component> {

  private ConcurrentLinkedQueue<Component> componentQueue;

  private volatile static ComponentStore componentStore;

  public ComponentStore() {
    this.componentQueue = new ConcurrentLinkedQueue<>();
  }

  public static ComponentStore getInstance() {
    if (componentStore == null) {
      synchronized (ComponentStore.class) {
        if (componentStore == null) {
          componentStore = new ComponentStore();
        }
      }
    }
    return componentStore;
  }

  public ConcurrentLinkedQueue<Component> getComponentQueue() {
    return componentQueue;
  }

  public void add(Component component) {
    this.componentQueue.add(component);
  }

  public Component poll() {
    return this.componentQueue.poll();
  }

  public int getCount() {
    return this.componentQueue.size();
  }

  public void saveComponent() {
    if (getCount() > 0) {
      new SaveService<Component>().saveToDISystem("components", ComponentStore.getInstance().getComponentQueue());
      System.out.println("Total: " + ComponentStore.getInstance().getComponentQueue().size());
    }
  }

  public void setComponentQueue(ConcurrentLinkedQueue<Component> componentQueue) {
    this.componentQueue = componentQueue;
  }
}
